
/**
 * 将数字转化成货币
 * @param num   传入的值
 * @param isDecimals    是否保留两位00小数 默认是
 * eg:utils.money(8.379999999999999) 结果 8.38
 * eg:utils.money(8.780000000000001) 结果 8.78
 * @returns {*}
 */
let money = function (num, isDecimals) {
	var sign, cents;
	if (isNaN(num)) num = "0";
	var reg = new RegExp('\$|', 'g');
	//num = num.toString().replace(/\$|\,/g, '');
	num = num.toString().replace(reg, '');
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
		num = num.substring(0, num.length - (4 * i + 3)) + '' + num.substring(num.length - (4 * i + 3));
	}
	var money_val = (((sign) ? '' : '-') + num + '.' + cents) * 1;
	if (isDecimals !== false) {
		return money_val.toFixed(2);
	} else {
		return money_val;
	}
};

export default money;
